package cn.com.scitc.licenseplateback.controller;

import cn.com.scitc.licenseplateback.dao.DetailsOfChargesDao;
import cn.com.scitc.licenseplateback.dao.LdleParkingDao;
import cn.com.scitc.licenseplateback.dao.MembershipInformationDao;
import cn.com.scitc.licenseplateback.model.DetailsOfChargesEntity;
import cn.com.scitc.licenseplateback.model.LdleParkingEntity;
import cn.com.scitc.licenseplateback.model.MembershipInformationEntity;
import cn.com.scitc.licenseplateback.redis.RedisUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class InitCubeInfo {

    /**
     * 查询各立方库的剩余车位信息
     */
    @Autowired
    private LdleParkingDao ldleParkingDao;

    /**
     * 缓存库
     */
    @Autowired
    private RedisUtil redisUtil;


    /**
     * 会员信息 CRUD
     */
    @Autowired
    private MembershipInformationDao membershipInformationDao;


    @Autowired
    private DetailsOfChargesDao detailsOfChargesDao;



    /**
     * 根据立方库编号 获取 立方库内的空闲车位信息
     * @param cubeLibraryNumber
     */
    @GetMapping("/initCubeInfo")
    private Object init(String cubeLibraryNumber){
        List<LdleParkingEntity> allByCubeInfo = ldleParkingDao.findAllByCubeInfo(cubeLibraryNumber);
        if (allByCubeInfo!=null)
        {
            List<List<String>> list = new ArrayList<>();
            for (LdleParkingEntity lpe:allByCubeInfo){
                List<String> sublist = new ArrayList<>();
                sublist.add(lpe.getRegion());
                sublist.add(String.valueOf(lpe.getArrangement()));
                sublist.add(lpe.getNumber());
                list.add(sublist);
            }
            return JSONObject.toJSON(list);
        }
        return "";
    }

    @PostMapping("/initUserInfo")
    private Object initUserInfo(HttpServletRequest request){
        String token = request.getHeader("cubelibrarytoken");
        Object tokenValue = redisUtil.get(token);
        if (tokenValue == null) return "";

        JSONArray jsonArray = JSONObject.parseArray(tokenValue.toString());
        JSONObject jsonObject = JSONObject.parseObject(jsonArray.get(0).toString());

        MembershipInformationEntity informationEntity = membershipInformationDao.findbyMember(jsonObject.get("userId").toString());

        List<Object> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("LicensePlateNumber",informationEntity.getLicensePlateNumber());
        map.put("Grade",informationEntity.getGrade());
        DetailsOfChargesEntity detailsOfChargesEntity = detailsOfChargesDao.findById(informationEntity.getGrade()).get();
        map.put("Price",detailsOfChargesEntity.getPrice());
        map.put("WaitFor",detailsOfChargesEntity.getWaitFor());
        map.put("BookingFee",detailsOfChargesEntity.getBookingFee());

        list.add(map);
        return JSONObject.toJSON(list);
    }
}
